<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加购物卡</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/temp/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/temp/css/frame.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>           
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>添加购物卡</legend>
</fieldset>

<form class="layui-form layui-form-pane" action="" lay-filter="example" style="padding: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label">所属店铺</label>
        <div class="layui-input-inline" style="width: 40%">
            <select name="admin_id">
                <option value="">-- 请选择店铺 --</option>
                {foreach $shop_list as $k => $v}
                <option value="{$v.id}">{$v.nickname}</option>
                {/foreach}
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="text" name="name" lay-verify="name" autocomplete="off" placeholder="请输入名称" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 长度1-30个字符
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">优惠金额</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="number" name="price" lay-verify="price" autocomplete="off" placeholder="请输入优惠金额" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">使用期限</label>
        <div class="layui-input-inline" style="width: 40%">
            <input type="number" name="time" lay-verify="time" placeholder="请输入使用期限" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">
            <span style="color: red">*</span> 天
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">适用类型</label>
        <div class="layui-input-inline" style="width: 40%">
            <select name="type" lay-filter="type">
                <option value="1" selected>无限制</option>
                <option value="2">分类</option>
                <option value="3">指定商品</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item" id="selectBox" style="display: none;">
        <label class="layui-form-label">适用范围</label>
        <div class="layui-input-inline" style="width: 40%">
            <select name="type_value" id="selectId">
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
            <a href="javascript:history.back(-1)" class="layui-btn layui-btn-primary">返回</a>
        </div>
    </div>
</form>
          
<script src="/static/temp/layui/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,layedit = layui.layedit;

        // 监听提交
        form.on('submit(submit)', function(data){
            if(!data.field.type_value && data.field.type == 1){
                data.field.type_value = 0;
            }

            // 加载中样式... 
            let loading = layer.msg('正在添加，请稍后...', {icon: 16, shade: 0.3, time:0});

            $.post('{:url("Coupon/cardShopAdd")}', data.field, function(res){
                if(res.code == 0){
                    layer.msg(res.msg, {icon: 1,time: 1000}, function(){
                        window.location.href = "{:url('Coupon/card_shop_list')}";
                    });
                }else{
                    layer.msg(res.msg, {icon: 2});
                }
            })

            return false;
        });

        // 下拉框事件，获取数据
        form.on('select(type)', function(data){
            // console.log(data.elem); //得到select原始DOM对象
            // console.log(data.value); //得到被选中的值
            // console.log(data.othis); //得到美化后的DOM对象
            if(data.value != 1){
                $("#selectBox").show()
                $.ajax({
                    type: 'post',
                    url: '{:url("Coupon/getCardShopTypeData")}',
                    data: {type: data.value},
                    dataType:'json',
                    success:function(res){
                        $("#selectId").html(`<option value="">-- 请选择 --</option>`);
                        res.data.forEach(val => {
                            $("#selectId").append(`<option value="${val.id}">${val.name}</option>`)
                        })

                        // 重新渲染下拉框,不能删,要不然下拉框没数据
                        form.render('select')
                    }
                })
            }else{
                $("#selectBox").hide()
            }
        });
    });
</script>
</body>
</html>